import { AppDispatch } from '..';
import {
  getUserInfoApi,
  getUserProfileApi,
  updateProfielApi,
} from '@/api/user';
import { updatePhotoApi } from './../../api/user';
import { ProfileType } from '../reducers/user';

/** 登录的Action */
export const getUserInfoAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserInfoApi();
    console.log('11,user', res);
    // 17.1.5  触发 传数据
    dispatch({ type: 'user/saveUser', payload: res.data });
  };
};

//17.2.2 个人资料action
/** 用户资料 */
export const getUserProfileAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserProfileApi();
    // console.log('profile', res);
    dispatch({ type: 'user/saveProfile', payload: res.data });
  };
};

//17.4.2
/** 编辑用户头像 */
export const updatePhotoAction = (data: FormData) => {
  return async (dispatch: AppDispatch) => {
    const res = await updatePhotoApi(data);
    console.log(res);
    // 18.1.3 触发 保存数据
    dispatch({ type: 'user/updateProfile', payload: res.data });
  };
};

// 18.3.2
/** 编辑用户个人资料 */
export const updateProfileAction = (data: Partial<ProfileType>) => {
  return async (dispatch: AppDispatch) => {
    await updateProfielApi(data);
    // console.log(45, res);
    // 18.3.5 存到redux 形参的data就是数据
    dispatch({ type: 'user/updateProfile', payload: data });
  };
};
